using HKB.Common;
using HKB.Data.Dal;
using HKB.Data.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
namespace HKB.Data.Bll
{
	public class AccountSubject
	{
		private readonly HKB.Data.Dal.AccountSubject dal = new HKB.Data.Dal.AccountSubject();
		public int GetMaxId()
		{
			return this.dal.GetMaxId();
		}
		public bool Exists(int AccountSubjectID)
		{
			return this.dal.Exists(AccountSubjectID);
		}
		public int Add(HKB.Data.Model.AccountSubject model)
		{
			return this.dal.Add(model);
		}
		public bool Update(HKB.Data.Model.AccountSubject model)
		{
			return this.dal.Update(model);
		}
		public bool Delete(int AccountSubjectID)
		{
			return this.dal.Delete(AccountSubjectID);
		}
		public bool DeleteList(string AccountSubjectIDlist)
		{
			return this.dal.DeleteList(AccountSubjectIDlist);
		}
		public HKB.Data.Model.AccountSubject GetModel(int AccountSubjectID)
		{
			return this.dal.GetModel(AccountSubjectID);
		}
		public HKB.Data.Model.AccountSubject GetModelByCache(int AccountSubjectID)
		{
			string cacheKey = "AccountSubjectModel-" + AccountSubjectID;
			object obj = DataCache.GetCache(cacheKey);
			if (obj == null)
			{
				try
				{
					obj = this.dal.GetModel(AccountSubjectID);
					if (obj != null)
					{
						int configInt = ConfigHelper.GetConfigInt("ModelCache");
						DataCache.SetCache(cacheKey, obj, System.DateTime.Now.AddMinutes((double)configInt), System.TimeSpan.Zero);
					}
				}
				catch
				{
				}
			}
			return (HKB.Data.Model.AccountSubject)obj;
		}
		public DataSet GetList(string strWhere)
		{
			return this.dal.GetList(strWhere);
		}
		public DataSet GetList(int Top, string strWhere, string filedOrder)
		{
			return this.dal.GetList(Top, strWhere, filedOrder);
		}
		public System.Collections.Generic.List<HKB.Data.Model.AccountSubject> GetModelList(string strWhere)
		{
			DataSet list = this.dal.GetList(strWhere);
			return this.DataTableToList(list.Tables[0]);
		}
		public System.Collections.Generic.List<HKB.Data.Model.AccountSubject> DataTableToList(DataTable dt)
		{
			System.Collections.Generic.List<HKB.Data.Model.AccountSubject> list = new System.Collections.Generic.List<HKB.Data.Model.AccountSubject>();
			int count = dt.Rows.Count;
			if (count > 0)
			{
				for (int i = 0; i < count; i++)
				{
					HKB.Data.Model.AccountSubject accountSubject = this.dal.DataRowToModel(dt.Rows[i]);
					if (accountSubject != null)
					{
						list.Add(accountSubject);
					}
				}
			}
			return list;
		}
		public DataSet GetAllList()
		{
			return this.GetList("");
		}
		public int GetRecordCount(string strWhere)
		{
			return this.dal.GetRecordCount(strWhere);
		}
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			return this.dal.GetListByPage(strWhere, orderby, startIndex, endIndex);
		}
		public string Display_ParentName(int id)
		{
			string text = "";
			string text2 = id.ToString();
			if (!string.IsNullOrEmpty(this.Display_ParentID(id)))
			{
				text2 = text2 + "," + this.Display_ParentID(id);
				if (text2.Contains(","))
				{
					DataTable dataTable = this.dal.GetList("AccountSubjectID in (" + text2 + ") order by AccountSParentID asc").Tables[0];
					int count = dataTable.Rows.Count;
					for (int i = 0; i < count; i++)
					{
						text = text + dataTable.Rows[i]["AccountSName"].ToString() + ",";
					}
					if (text != "")
					{
						text = text.Substring(0, text.Length - 1).Replace(",", "  >>  ");
					}
				}
			}
			else
			{
				HKB.Data.Model.AccountSubject model = this.dal.GetModel(id);
				text = model.AccountSName;
			}
			return text;
		}
		private string Display_ParentID(int id)
		{
			string text = "";
			HKB.Data.Model.AccountSubject model = this.dal.GetModel(id);
			if (model != null)
			{
				if (model.AccountSParentID > 0)
				{
					text = text + model.AccountSParentID + ",";
					int i = System.Convert.ToInt32(model.AccountSParentID.ToString());
					while (i > 0)
					{
						HKB.Data.Model.AccountSubject model2 = this.dal.GetModel(i);
						if (model2 != null)
						{
							if (model2.AccountSParentID.ToString() != "0")
							{
								text = text + model2.AccountSParentID.ToString() + ",";
							}
							i = System.Convert.ToInt32(model2.AccountSParentID.ToString());
						}
					}
				}
			}
			if (text != "")
			{
				text = text.Substring(0, text.Length - 1);
			}
			return text;
		}
		public string GetSubjcetNameBySubjectID(int subjcetID)
		{
			string result = "";
			DataTable dataTable = this.dal.GetSubjcetNameBySubjectID(subjcetID).Tables[0];
			if (dataTable.Rows.Count > 0)
			{
				if (dataTable.Columns.Contains("AccountSName"))
				{
					if (dataTable.Rows[0]["AccountSName"] != System.DBNull.Value)
					{
						result = dataTable.Rows[0]["AccountSName"].ToString();
					}
				}
			}
			return result;
		}
		public HKB.Data.Model.AccountSubject GetModelByName(string accountSubjectName)
		{
			return this.dal.GetModelByName(accountSubjectName);
		}
		public void BindSubjcet(DropDownList ddl, int parentID, int depthNum)
		{
			string strWhere = "AccountSParentID=" + parentID + "  order by AccountSRootID,AccountSParentID desc";
			DataTable dataTable = BLLInstance.BllAccountSubject.GetList(strWhere).Tables[0];
			foreach (DataRow dataRow in dataTable.Rows)
			{
				string value = dataRow["AccountSubjectID"].ToString();
				string str = dataRow["AccountSNumber"].ToString() + " " + dataRow["AccountSName"].ToString();
				int depth = System.Convert.ToInt32(dataRow["AccountSDepth"].ToString());
				ListItem listItem = new ListItem();
				listItem.Value = value;
				listItem.Text = this.ShowSubjectPath(depth, depthNum) + str;
				ddl.Items.Add(listItem);
				this.BindSubjcet(ddl, System.Convert.ToInt32(value), depthNum);
			}
		}
		public void BindSubjcet(DropDownList ddl, int parentID, int depthNum, string sqlWhere)
		{
			string strWhere = string.Concat(new object[]
			{
				"AccountSParentID=",
				parentID,
				sqlWhere,
				"  order by AccountSRootID,AccountSParentID desc"
			});
			DataTable dataTable = BLLInstance.BllAccountSubject.GetList(strWhere).Tables[0];
			foreach (DataRow dataRow in dataTable.Rows)
			{
				string value = dataRow["AccountSubjectID"].ToString();
				string str = dataRow["AccountSNumber"].ToString() + " " + dataRow["AccountSName"].ToString();
				int depth = System.Convert.ToInt32(dataRow["AccountSDepth"].ToString());
				ListItem listItem = new ListItem();
				listItem.Value = value;
				listItem.Text = this.ShowSubjectPath(depth, depthNum) + str;
				ddl.Items.Add(listItem);
				this.BindSubjcet(ddl, System.Convert.ToInt32(value), depthNum, sqlWhere);
			}
		}
		private string ShowSubjectPath(int depth, int depthNum)
		{
			string text = "";
			for (int i = depthNum + 1; i < depth; i++)
			{
				text += "--";
			}
			if (depth > depthNum)
			{
				text += "└";
			}
			return text;
		}
	}
}
